﻿using System;
using System.Reactive.Disposables;
using Avalonia.ReactiveUI;
using Generators.Sandbox.ViewModels;
using ReactiveUI;
using ReactiveUI.Validation.Extensions;
using ReactiveUI.Validation.Formatters;

namespace Generators.Sandbox.Controls;

/// <summary>
/// This is a sample view class with typed x:Name references generated using
/// .NET 5 source generators. The class has to be partial because x:Name
/// references are living in a separate partial class file. See also:
/// https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/
/// </summary>
public partial class SignUpView : ReactiveUserControl<SignUpViewModel>
{
    public SignUpView()
    {
        // The InitializeComponent method is also generated automatically
        // and lives in the autogenerated part of the partial class.
        InitializeComponent();
        this.WhenActivated(disposables =>
        {
            this.Bind(ViewModel, x => x.UserName, x => x.UserNameTextBox.Text)
                .DisposeWith(disposables);
            this.Bind(ViewModel, x => x.Password, x => x.PasswordTextBox.Text)
                .DisposeWith(disposables);
            this.Bind(ViewModel, x => x.ConfirmPassword, x => x.ConfirmPasswordTextBox.Text)
                .DisposeWith(disposables);
            this.BindCommand(ViewModel, x => x.SignUp, x => x.SignUpButton)
                .DisposeWith(disposables);

            this.BindValidation(ViewModel, x => x.UserName, x => x.UserNameValidation.Text)
                .DisposeWith(disposables);
            this.BindValidation(ViewModel, x => x.Password, x => x.PasswordValidation.Text)
                .DisposeWith(disposables);
            this.BindValidation(ViewModel, x => x.ConfirmPassword, x => x.ConfirmPasswordValidation.Text)
                .DisposeWith(disposables);

            var newLineFormatter = new SingleLineFormatter(Environment.NewLine);
            this.BindValidation(ViewModel, x => x.CompoundValidation.Text, newLineFormatter)
                .DisposeWith(disposables);

            // The references to text boxes below are also auto generated.
            // Use Ctrl+Click in order to view the generated sources. 
            UserNameTextBox.Text = "Joseph!";
            PasswordTextBox.Text = "1234";
            ConfirmPasswordTextBox.Text = "1234";
            SignUpButtonDescription.Text = "Press the button below to sign up.";
        });
    }
}
